package com.ellfors.common.http.utils

import com.ellfors.common.http.entity.BaseException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 * 协程配置类
 */
class CoroutinesOptions<T>(val mScope: CoroutineScope) {

    var success: (suspend (T?) -> Unit)? = null
    var error: (suspend (BaseException) -> Unit)? = null
    var start: (suspend () -> Unit)? = null
    var complete: (suspend () -> Unit)? = null

    fun triggerSuccess(data: T?) {
        mScope.launch {
            success?.invoke(data)
        }
    }

    fun triggerError(e: BaseException) {
        mScope.launch {
            error?.invoke(e)
        }
    }

    fun triggerStart() {
        mScope.launch {
            start?.invoke()
        }
    }

    fun triggerComplete() {
        mScope.launch {
            complete?.invoke()
        }
    }
}